<?php

namespace App\Http\Controllers;

use App\Models\Parish;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

class ParishController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $model = Parish::query();

        if(request()->input('title')){
            $model->where('title','like','%'.request()->input('title').'%');
        }
        return new ResourceCollection($model->paginate());
    }

    public function getlist(Request $request){
        $model = Parish::query();
        $diocese_id = $request->input('diocese_id');
        if($diocese_id){
            $model->where('diocese_id',$diocese_id);
        }
        $list = $model->select('id','title')->get();
        return new ResourceCollection($list);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //


    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $attr = $request->all();
        $attr['admin_user_id'] = auth()->id();
        Parish::create($attr);
//        dump($request->all());
        return $this->created();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Parish $parish)
    {
        //
        return $parish;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $attr = $request->all();
//        $attr['admin_user_id'] = auth()->id();
        $attr['updated_at'] = now();
        Parish::findOrFail($id)->update($attr);
        return $this->noContent();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        Parish::findOrFail($id)->delete();
        return $this->noContent();
    }
}
